﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Pheonix.Fashionzoomer.Entity.SqlServer2000;
using System.Collections.Generic;
using Pheonix.Fashionzoomer.DAL;
using Pheonix.CodeLibrary.Data;
using System.Text;
using System.IO;
using Pheonix.CodeLibrary;

/// <summary>
/// Summary description for ShopManager
/// </summary>
public sealed class ShopManager
{
    private ShopManager(){}

    public static List<Shop> GetShopSearchResult(string brand,string shopId,int category,string keywords)
    {
        List<Shop> result = new List<Shop>();

        DataTable tempResult = CustomCommand.GetShopListBySearch(brand, shopId, category, keywords);

        foreach (DataRow row in tempResult.Rows)
        {
            try
            {
                Shop shop = new Shop();
                shop.Id = Convert.ToInt32(row["id"]);
                shop.ShopName = row["shop_name"].ToString();
                shop.ShopCode = row["shop_code"].ToString();
                shop.Rating = Convert.ToDecimal(row["rating"]);
                shop.ShopLogoPath = row["shop_logo_path"].ToString();
                shop.BranchAddress = row["branch_address"].ToString();

                result.Add(shop);
            }
            catch (Exception ex)
            {
                // nothing to do, just skip this shop if any error
                // but send the error to logger
            }
        }

        return result;
    }

    public static DataTable GetShopCategoryList(int shopId)
    {
        DataTable result = CustomCommand.GetShopCategoryByShopId(shopId);

        return result;
    }

    public static CustomCollection<Shop> GetFeaturedShops()
    {
        DataTable shops = CustomCommand.GetFeaturedShopList();
        CustomCollection<Shop> featuredShops = new CustomCollection<Shop>();
        foreach (DataRow row in shops.Rows)
        {
            Shop shop = new Shop();
            shop.ShopLogoPath = row["shop_logo_path"] == null ? String.Empty : row["shop_logo_path"].ToString();
            shop.ShopName = row["shop_name"] == null ? String.Empty : row["shop_name"].ToString();
            shop.Id = row["id"] == null ? Int32.MinValue : Convert.ToInt32(row["id"]);

            featuredShops.Add(shop);
        }
        return featuredShops;
    }

    public static DataTable GetShopDetail(int shopId)
    {
        DataTable result = CustomCommand.GetShopDetail(shopId);

        return result;
    }

    public static DataTable GetShopNewProducts(int shopId)
    {
        DataTable result = CustomCommand.GetShopProducts(shopId);

        return result;
    }

    public static bool DeleteShopImages(List<int> idToDelete)
    {
        try
        {
            CustomCollection<Shop> shops = CustomCommand.GetShopsById(idToDelete);
            if (shops != null)
            {
                for (int i = 0; i < shops.Count; i++)
                {
                    string imageUrl = ((Shop)shops[i]).ShopLogoPath;
                    string folderPath = imageUrl.Substring(0, imageUrl.LastIndexOf('/'));

                    if (Directory.Exists(HttpContext.Current.Server.MapPath(folderPath)))
                        FileAccessUtility.DeleteFolder(folderPath);
                }
                return true;
            }
            return false;
        }
        catch
        {
            return false;
        }
    }

    public static string UploadShopImage(string temporaryImageLocation, string shopCode)
    {
        try
        {
            string path;
            string extension = Path.GetExtension(temporaryImageLocation);
            string imageBasePath = "~/upload/image/shoplogo/";
            string imagePath = string.Empty;

            StringBuilder imageUrl = new StringBuilder();
            imageUrl = imageUrl.Append(imageBasePath).Append(shopCode).Append("/");
            imageUrl = imageUrl.Append(shopCode).Append("_max").Append(extension);

            imagePath = HttpContext.Current.Server.MapPath(imageUrl.ToString());

            string shopFolderPath = imageBasePath + shopCode;
            DirectoryInfo shopFolderInfo = new DirectoryInfo(shopFolderPath);
            if (!shopFolderInfo.Exists)
                FileAccessUtility.CreateFolder(shopFolderPath);
            else
            {
                FileAccessUtility.DeleteFolder(shopFolderPath);
                FileAccessUtility.CreateFolder(shopFolderPath);
            }

            string shopThumbImage = imageUrl.ToString().Replace("max", "thumb");

            if (ImageResizer.SaveAs(temporaryImageLocation, imageUrl.ToString(),
                Utility.GetImageFormat(extension, true)) &&
                ImageResizer.CreateThumbnail(imageUrl.ToString(),
                    shopThumbImage, 100, false, null))
            {
                path = shopThumbImage;
                FileAccessUtility.DeleteFile(temporaryImageLocation);
            }
            else
                path = null;

            return path;
        }
        catch
        {
            return null;
        }
    }

}
